package com.iking.toolkit.system.controller;

import com.iking.toolkit.model.Msg;
import com.iking.toolkit.model.PageRes;
import com.iking.toolkit.system.model.http.HttpDictionaryItemSelectReq;
import com.iking.toolkit.system.model.model.DictionaryItems;
import com.iking.toolkit.system.service.DictionaryItemsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 数据字典内容管理
 *
 * @author wk
 * @date 7/2/2020 上午9:07
 */
@RestController
@RequestMapping("/sys/dictionaryItems")
@Tag(name = "DictionaryItemsController", description = "数据字典内容管理")
@Slf4j
public class DictionaryItemsController {

    /**
     * 数据字典内容管理
     */
    @Autowired
    private DictionaryItemsService dictionaryItemsService;

    /**
     * @Description: 根据类型查询内容
     * @Param: [req]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:07
     */
    @Operation(summary="根据类型查询内容")
    @RequestMapping(value = "get/typeId", method = RequestMethod.POST)
    public Msg<PageRes<DictionaryItems>> searchDictionaryItemsByTypeId(@RequestBody HttpDictionaryItemSelectReq req) {
        try {
            //参数非空校验
            if (req == null) {
                return Msg.fail("查询参数为空");
            }
            //字典内容非空校验
            if (!StringUtils.isBlank(req.getDicContent())) {
                //去除空格
                req.setDicContent(req.getDicContent().replaceAll(" ", ""));
            }
            return dictionaryItemsService.searchInstrumentsId(req);
        } catch (Exception e) {
            log.error("根据类型查询内容异常,异常信息：" + e);
            return Msg.fail("根据类型查询内容异常");
        }
    }

    /**
     * @Description: 根据参数查询内容 Msg
     * @Param: [pym]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:25
     */
    @Operation(summary="根据参数查询内容")
    @RequestMapping(value = "get/pym/{pym}", method = RequestMethod.GET)
    public Msg searchDictionaryItemsByPrm(@PathVariable("pym") String pym) {
        try {
            //参数非空校验
            if (StringUtils.isBlank(pym)) {
                return Msg.fail("拼音码为空");
            }
            return dictionaryItemsService.searchInstrumentsPym(pym);
        } catch (Exception e) {
            log.error("根据参数查询内容异常,异常信息：" + e);
            return Msg.fail("根据参数查询内容异常");
        }
    }

    /**
     * @Description: 新增数据字典内容
     * @Param: [req]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:27
     */
    @Operation(summary="新增数据字典内容")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Msg insert(@RequestBody DictionaryItems req) {
        try {
            //参数非空校验
            if (req == null) {
                return Msg.fail("新增内容为空");
            }
            if (StringUtils.isBlank(req.getDictionaryTypeId())) {
                return Msg.fail("数据字典类型ID为空");
            }
            //去除两端的空格
            req.setDictionaryTypeId(req.getDictionaryTypeId().trim());
            if (StringUtils.isBlank(req.getItemName())) {
                return Msg.fail("数据字典内容名称为空");
            }else{
                if(req.getItemName().length() > 50){
                    return Msg.fail("字典内容最大可以输入50个字符");
                }
            }
            req.setItemName(req.getItemName().trim());
            if (StringUtils.isBlank(req.getItemValue())) {
                return Msg.fail("数据字典内容值为空");
            }else{
                if(req.getItemValue().length() > 200){
                    return Msg.fail("字典内容最大可以输入200个字符");
                }
            }
            req.setItemValue(req.getItemValue().trim());
            if (StringUtils.isBlank(req.getItemPym())) {
                return Msg.fail("参数为空");
            }else{
                if(req.getItemPym().length() > 50){
                    return Msg.fail("参数最大可以输入50个字符");
                }
            }
            if (StringUtils.isNotBlank(req.getDescription()) && req.getDescription().length() > 200) {
                return Msg.fail("备注最大可以输入200个字符");
            }
            req.setItemPym(req.getItemPym().trim());
            return dictionaryItemsService.insert(req);
        } catch (Exception e) {
            log.error("新增数据字典内容异常,异常信息：" + e);
            return Msg.fail("新增数据字典内容异常");
        }

    }


    /**
     * @Description: 修改数据字典内容
     * @Param: [req]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:28
     */
    @Operation(summary="修改数据字典内容")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Msg updateDictionaryItems(@RequestBody DictionaryItems req) {
        try {
            if (req == null) {
                return Msg.fail("修改内容为空");
            }
            if (StringUtils.isBlank(req.getItemName())) {
                return Msg.fail("数据字典内容名称为空");
            }else{
                if(req.getItemName().length() > 50){
                    return Msg.fail("字典内容最大可以输入50个字符");
                }
            }
            if (StringUtils.isBlank(req.getItemValue())) {
                return Msg.fail("数据字典内容值为空");
            }else{
                if(req.getItemValue().length() > 200){
                    return Msg.fail("字典内容最大可以输入200个字符");
                }
            }
            if (StringUtils.isBlank(req.getItemPym())) {
                return Msg.fail("参数为空");
            }else{
                if(req.getItemPym().length() > 25){
                    return Msg.fail("参数最大可以输入25个字符");
                }
            }
            if (StringUtils.isNotBlank(req.getDescription()) && req.getDescription().length() > 200) {
                return Msg.fail("备注最大可以输入200个字符");
            }
            if (req.getDictionaryTypeId() != null && "".equals(req.getDictionaryTypeId().replaceAll(" ", ""))) {
                req.setDictionaryTypeId(null);
            }
            if (req.getItemName() != null && "".equals(req.getItemName().replaceAll(" ", ""))) {
                req.setItemName(null);
            }
            if (req.getItemValue() != null && "".equals(req.getItemValue().replaceAll(" ", ""))) {
                req.setItemValue(null);
            }
            if (req.getItemPym() != null && "".equals(req.getItemPym().replaceAll(" ", ""))) {
                req.setItemPym(null);
            }
            if (req.getIsDelete() != null && "".equals(req.getIsDelete().toString().replaceAll(" ", ""))) {
                req.setIsDelete(null);
            }
            if (req.getIsEnable() != null && "".equals(req.getIsEnable().toString().replaceAll(" ", ""))) {
                req.setIsEnable(null);
            }
            return dictionaryItemsService.update(req);
        } catch (Exception e) {
            log.error("修改数据字典内容异常,异常信息：" + e);
            return Msg.fail("修改数据字典内容异常");
        }
    }

    /**
     * @Description: 删除数据字典内容
     * @Param: [id]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:30
     */
    @Operation(summary="删除数据字典内容")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public Msg deleteByID(@PathVariable("id") String id) {
        try {
            if (StringUtils.isBlank(id)) {
                return Msg.fail("数据字典内容ID为空");
            }
            return dictionaryItemsService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            log.error("删除数据字典内容异常,异常信息：" + e);
            return Msg.fail("删除数据字典内容异常");
        }
    }

    /**
     * @Description: 根据数据字典类型拼音码和数据字典内容拼音码查询数据字典内容
     * @Param: [valuepym, typepym]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:31
     */
    @Operation(summary="根据数据字典类型拼音码和数据字典内容拼音码查询数据字典内容")
    @RequestMapping(value = "/get/typeIdAndPym/{valuePym}/{typePym}", method = RequestMethod.GET)
    public Msg selectByTypeIdAndPym(@PathVariable("valuePym") String valuePym,
                                    @PathVariable("typePym") String typePym) {
        try {
            return dictionaryItemsService.selectByTypeIdAndPym(valuePym, typePym);
        } catch (Exception e) {
            log.error("根据拼音码查询异常,异常信息：" + e);
            return Msg.fail("根据拼音码查询异常");
        }
    }

    @Operation(summary="根据数据字典类型拼音码和数据字典内容拼音码查询数据字典内容")
    @RequestMapping(value = "/get/getByTypeIdAndPym", method = RequestMethod.GET)
    public Msg<DictionaryItems> getByTypeIdAndPym(@RequestParam("valuePym") String valuePym,
                                                  @RequestParam("typePym") String typePym) {
        try {
            return dictionaryItemsService.selectByTypeIdAndPym(valuePym, typePym);
        } catch (Exception e) {
            log.error("根据拼音码查询异常,异常信息：" + e);
            return Msg.fail("根据拼音码查询异常");
        }
    }

    /**
     * @Description: 根据数据字典类型拼音码查询数据字典内容
     * @Param: [typepym]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:32
     */
    @Operation(summary="根据数据字典类型拼音码查询数据字典内容")
    @RequestMapping(value = "/get/typePym/{typePym}", method = RequestMethod.GET)
    public Msg selectByTypePym(@PathVariable("typePym") String typePym) {
        try {
            return dictionaryItemsService.selectByTypePym(typePym);
        } catch (Exception e) {
            log.error("根据数据字典类型拼音码查询数据字典内容异常,异常信息：" + e);
            return Msg.fail("根据数据字典类型拼音码查询数据字典内容异常");
        }

    }

    @Operation(summary="根据数据字典类型拼音码查询数据字典内容")
    @RequestMapping(value = "/get/typePym", method = RequestMethod.GET)
    public Msg getByTypePym(@RequestParam("typePym") String typePym) {
        try {
            return dictionaryItemsService.selectByTypePym(typePym);
        } catch (Exception e) {
            log.error("根据数据字典类型拼音码查询数据字典内容异常,异常信息：" + e);
            return Msg.fail("根据数据字典类型拼音码查询数据字典内容异常");
        }

    }

}
